package com.hitqz.robot.camera.listener;

import com.google.common.collect.Table;
import com.hitqz.robot.api.business.event.EquipmentPoseUpdateEvent;
import com.hitqz.robot.api.common.entity.RobotEquipmentPose;
import com.hitqz.robot.camera.HikVisionService;
import com.hitqz.robot.camera.entity.HkCameraDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Objects;


@Component
@Slf4j
public class CameraEventListener {

    @Autowired
    private HikVisionService hkCamera;

    //测温初始化设置
    @Async("cameraInitExecutor")
    @EventListener
    public void onHkInitHotEvent(HkCameraDriver cameraDriver) {
        synchronized (cameraDriver.getIp().intern()) {
            //获取测温能力集
            if (hkCamera.getThermalCap(cameraDriver)){
               //获取测温模式能力集
                hkCamera.getThermometryMode(cameraDriver);
               //设置测温模式
                hkCamera.initThermometryMode(cameraDriver);
                //设置区域
                hkCamera.setRegion(cameraDriver,null,true);
                //ptz能力集
                hkCamera.getPTZCap(cameraDriver);
                hkCamera.getPTZAbsoluteExCap(cameraDriver);
                hkCamera.getImageCap(cameraDriver);
           }
        }
    }


    @Async("cameraInitExecutor")
    @EventListener
    public void onUpdatePoseEvent(EquipmentPoseUpdateEvent event){
        if (!Objects.equals(event.getBrand().toUpperCase(),hkCamera.brand().getEnStr().toUpperCase())){
            return;
        }
        //删除旧值
        String ip = event.getIp();
        Table<String, String, String> paramsCacheTable = hkCamera.getParamsCacheTable();
        paramsCacheTable.rowMap().remove(ip);
        //缓存新的值
        for (RobotEquipmentPose pose : event.getPoseList()) {
            paramsCacheTable.put(ip,pose.getTypeStr(),pose.getJson());
        }

    }


}
